package org.example.javawebday1.controller;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import org.example.javawebday1.pojo.Emp;
import org.example.javawebday1.pojo.Result;
import org.example.javawebday1.service.EmpsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    EmpsService empsService;


    @PostMapping
    public Result login(@RequestBody Emp emp) {
        return Result.success(empsService.empLogin(emp.getUsername(), emp.getPassword()));
    }

    @GetMapping
    public Result login(String username, String password,  HttpServletResponse response) {
        String token = empsService.empLogin2(username,password).getToken();
        Cookie cookie = new Cookie("token", token);
        cookie.setHttpOnly(false);  // 安全性考虑
        cookie.setMaxAge(7 * 24 * 60 * 60); // 7天有效期
        cookie.setPath("/emps");  // 设置路径
        response.addCookie(cookie);
        return Result.success(token);
    }

}
